package com.credithc.eagleeye.plugins.mvc;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2019年10月16日<br/>
 * 版 本：v1.0.9<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 * @since 1.0.9
 */
@WebFilter(filterName = "traceFilter", urlPatterns = "/*")
public class EagleEyeMvcFilter implements Filter {
    private static final String FORM_CONTENT_TYPE = "application/x-www-form-urlencoded";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        ServletRequest requestWrapper = null;
        if (request instanceof HttpServletRequest) {
            HttpServletRequest httpServletRequest = (HttpServletRequest) request;
            String method = httpServletRequest.getMethod();
            String contentType = httpServletRequest.getContentType();
            if ((contentType != null) && (contentType.contains("multipart/form-data")) && ("POST".equalsIgnoreCase(method))) {
                //上传文件忽略
            }else{
                requestWrapper = new ContentCachingRequestWrapper(httpServletRequest);
            }
        }
        if (requestWrapper != null) {
            chain.doFilter(requestWrapper, response);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }
}
